package com.hz.zuulgateway.filter;

import com.hz.zuulgateway.utils.AESUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 对所有请求的参数进行解密
 */
@Component
public class RequestDencFilter extends ZuulFilter {

    /**
     * 过滤器的类型
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /***
     * 执行顺序
     * @return
     */
    @Override
    public int filterOrder() {


        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        System.out.println("------------run-------------");

        //业务代码
        //1.获取请求参数 HttpServeletRequest

//        HttpServeletRequest request = null;

        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();


        //2.获取请求参数
        String orderId = request.getParameter("orderId");


        String oo = null;

        try {
            //3.进行解密
            oo =  AESUtil.Decrypt(orderId,"1234567890098769");
        } catch (Exception e) {
            e.printStackTrace();
        }


        //4.交给ZUUL继续执行



        return null;
    }
}
